package com.pay.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.exceptions.JedisConnectionException;


/**
 * @Description: Redis工具类
 * @see: RedisUtil 此处填写需要参考的类
 * @version 2016年11月2日 上午11:28:09
 * @author shulin.feng
 */
public class RedisUtil {

	private static JedisSentinelPool jedisSentinelPool;

	/**
	 * @Description 查询缓存中key的值
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			result = jedis.get(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return result;
	}

	/**
	 * @Description 设置缓存
	 * @param key
	 * @param value
	 * @param expire
	 
	 */
	public static void setKeyValue(String key, String value, int expire) {

		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			jedis.set(key, value);
			if (expire > 0) {
				jedis.expire(key, expire);
			}
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}
	
	/**
	 * @Description 设置缓存
	 * @param key
	 * @param value
	 * @param expire
	 */
	public static int setnx(String key, String value, int expire) {

		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			Long setnx = jedis.setnx(key, value);
			if (1==setnx) {
				if (expire > 0) {
					jedis.expire(key, expire);
				}
				return 1;
			}else{
				return 0;
			}
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}

	/**
	 * @Description key失效
	 * @param key
	 * @param expire
	 */
	public static void expire(String key, int expire) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			jedis.expire(key, expire);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}


	/**
	 * @Description 删除key
	 * @param key
	 * @see 
	 */
	public static void removeKeyValue(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			jedis.del(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}

	/**
	 * @Description 通过key向list左头部添加字符串
	 * @param key
	 * @param strs
	 * @see 
	 */
	public static Long lpush(String key, String... strs) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.lpush(key, strs);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key从list右尾部删除一个value,并返回该元素
	 * @param key
	 * @return
     */
	public static String rpop(String key) {
		Jedis jedis = null;
		String res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.rpop(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key查询队列key的长度
	 * @param key
	 * @return
	 * @see 
	 */
	public static Long llen(String key) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.llen(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key删除
	 * @param key
	 * @return
	 * @see 
	 */
	public static Long del(String key) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.del(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key向指定value追加值
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 * @see 
	 */
	public static Long append(String key, String value, int expire) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.append(key, value);
			if (expire > 0) {
				jedis.expire(key, expire);
			}
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key向指定的set中添加value
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 * @see 
	 */
	public static Long sadd(String key, String value, int expire) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.sadd(key, value);
			if (expire > 0) {
				jedis.expire(key, expire);
			}
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key获取set中所有的value
	 */
	public static Set<String> smembers(String key) {
		Jedis jedis = null;
		Set<String> res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.smembers(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 返回满足pattern表达式的所有key
	 * @param pattern
	 *            key表达式
	 * @return
	 * @see 
	 */
	public static Set<String> keys(String pattern) {
		Jedis jedis = null;
		Set<String> res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.keys(pattern);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * @Description 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1
	 * @param key
	 * @return
	 * @see 
	 */
	public static Long incr(String key, int expire) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.incr(key);
			if (expire > 0) {
				jedis.expire(key, expire);
			}
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key给field设置指定的值,如果key不存在,则先创建
	 * 
	 * @param key
	 * @param field
	 *            字段
	 * @param value
	 * @return 如果存在返回0 异常返回null
	 */
	public static Long hset(String key, String field, String value) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.hset(key, field, value);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key 删除指定的 field
	 * 
	 * @param key
	 * @param fields
	 *            可以是 一个 field 也可以是 一个数组
	 * @return
	 */
	public static Long hdel(String key, String... fields) {
		Jedis jedis = null;
		Long res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.hdel(key, fields);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key获取所有的field和value
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, String> hgetall(String key) {
		Jedis jedis = null;
		Map<String, String> res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.hgetAll(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key返回所有和key有关的value
	 * 
	 * @param key
	 * @return
	 */
	public static List<String> hvals(String key) {
		Jedis jedis = null;
		List<String> res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.hvals(key);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}

	/**
	 * 通过key 和 field 获取指定的 value
	 * 
	 * @param key
	 * @param field
	 * @return 没有返回null
	 */
	public static String hget(String key, String field) {
		Jedis jedis = null;
		String res = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			res = jedis.hget(key, field);
		} catch (JedisConnectionException e) {
			if (jedis != null) {
				jedisSentinelPool.returnBrokenResource(jedis);
				jedis = null;
			}
			throw e;
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return res;
	}
	public static boolean exist(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			return jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
		return false;
	}
	public static String get(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			String result = jedis.get(key);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}
	public static void set(String key, String value, int expire) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			jedis.set(key, value);
			if (expire > 0) {
				jedis.expire(key, expire);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}
	public static void set(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedisSentinelPool().getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedisSentinelPool.returnResource(jedis);
			}
		}
	}
	public static JedisSentinelPool getJedisSentinelPool() {
		if (jedisSentinelPool == null) {
			jedisSentinelPool = (JedisSentinelPool) ApplicationContextHandle.getBean("jedisSentinelPool");
		}
		return jedisSentinelPool;
	}
}
